package vip.xiaonuo.statistics.vehicle;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.vehicle.modular.vehicleinfo.entity.VehicleInfo;
import vip.xiaonuo.vehicle.modular.vehicleinfo.service.VehicleInfoService;
import vip.xiaonuo.vehicle.modular.vehicletrafficrecord.service.VehicleTrafficRecordService;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆api
 *
 * @author cad
 * @date 2023/04/06
 */
@RestController
@RequestMapping("/vehicleApi")
public class VehicleApi {
    @Resource
    private VehicleInfoService vehicleInfoService;
    @Resource
    private VisitorService visitorService;
    @Resource
    private VehicleTrafficRecordService vehicleTrafficRecordService;
    final static ThreadLocal<DecimalFormat> DECIMAL_FORMAT_THREAD_LOCAL = ThreadLocal.withInitial(() -> new DecimalFormat("##.00%"));

    /**
     * 查询总量
     *
     * @return {@link ResponseData}
     */
    @GetMapping("/getTotalQuantity")
    public ResponseData getTotalQuantity() {
        //内部人员车辆总数
        int memberVehicleCount = new Long(vehicleInfoService.count(
                new LambdaQueryWrapper<VehicleInfo>().isNotNull(VehicleInfo::getLicensePlateNumber))).intValue();
        //访客车辆总数
        int visitorVehicleCount = new Long(visitorService.count(
                new LambdaQueryWrapper<Visitor>().isNotNull(Visitor::getCarNum))).intValue();
        //所有类型车辆总数
        int totalQuantity = memberVehicleCount + visitorVehicleCount;
        double memberVehiclePercentage = (double) memberVehicleCount / totalQuantity;
        double visitorVehiclePercentage = (double) visitorVehicleCount / totalQuantity;
        DecimalFormat df = DECIMAL_FORMAT_THREAD_LOCAL.get();
        //内部人员车辆百分比
        df.format(memberVehiclePercentage);
        //访客车辆百分比
        df.format(visitorVehiclePercentage);
        return ResponseData.success();
    }

    /**
     * 通过时间查询数量
     *
     * @return {@link ResponseData}
     */
    @GetMapping("/getQuantityByTime")
    public ResponseData getQuantityByTime() {
        Map<String,List<Map<String, String>>> map = new HashMap<>();
        LocalDateTime now = LocalDateTime.now();
        Date date = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        List<Map<String, String>> memberTrafficByTime = vehicleTrafficRecordService.selectMemberTrafficByTime(date);
        List<Map<String, String>> visitorTrafficByTime = vehicleTrafficRecordService.selectVisitorTrafficByTime(date);
        map.put("member",memberTrafficByTime);
        map.put("visitor",visitorTrafficByTime);
        return ResponseData.success(map);
    }
}
